package com.nbjtjc.safe.api.edu;

import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;

import com.nbjtjc.safe.annotation.SysLog;
import com.nbjtjc.safe.bean.Result;
import com.nbjtjc.safe.mapper.sys.SysUserMapper;
import com.nbjtjc.safe.model.cost.CostBudgetPlan;
import com.nbjtjc.safe.model.edu.EduPlanList;
import com.nbjtjc.safe.model.sys.SysFile;
import com.nbjtjc.safe.model.sys.SysNotice;
import com.nbjtjc.safe.service.edu.EduPlanListService;
import com.nbjtjc.safe.service.sys.SysNoticeService;
import com.nbjtjc.safe.util.MyShiroUtils;

import cn.hutool.core.util.ObjectUtil;
import io.swagger.annotations.ApiOperation;

@RestController
@CrossOrigin("*")
@RequestMapping("/eduPlan")
public class EduPlanListController {
	@Autowired
	private EduPlanListService eduPlanListService;
	@Autowired
	private SysUserMapper sysUserMapper;
	@Autowired
	private SysNoticeService sysNoticeService;


	  @RequestMapping(value = "/addOrUpdate",method = {RequestMethod.POST})
	  @SysLog("添加或更新培训计划")
	    public Result add(@RequestBody EduPlanList eduPlanList){
	        Result result=new Result();
	        try {
	            result=eduPlanListService.addByTemplet(eduPlanList);
		        //发通知，刘杰1127
	            if(ObjectUtil.isNotNull(eduPlanList)&&ObjectUtil.isNotNull(eduPlanList.getId())){
	        		SysNotice sysNotice = new SysNotice();
		            sysNotice.setContent("年度培训计划已发布");
		            sysNotice.setCompanyId(MyShiroUtils.getCompanyId());
		            sysNotice.setWriteUserId(MyShiroUtils.getSessionUser().getUserId());
		            sysNotice.setDeptId(MyShiroUtils.getDeptId());
		            sysNotice.setCreateTime(new Date());
		            sysNotice.setIdType(8);
		            sysNotice.setRelateId(eduPlanList.getId());
		            sysNotice.setNoticeType(0);
		            sysNotice.setTitle("年度培训计划已发布");
		            List<Integer> companyIds= new ArrayList();
		            companyIds.add(MyShiroUtils.getCompanyId());
		            List<Integer> userIds = sysUserMapper.findUserIdsByCompanyIds(companyIds);
		            boolean f = sysNoticeService.addNoticeList(sysNotice,userIds);
		            if(!f){
		            	result.setFalse("通知发送失败");
		            }
	            }

	        } catch (Exception e) {
	            e.printStackTrace();
	            result.setFalse(e.getMessage());
	        }
	        return result;
	    }

	 
	  @RequestMapping(value = "/delete",method = {RequestMethod.POST})
	  @SysLog("删除培训计划")
	    public Result delete(@RequestBody  EduPlanList eduPlanList) {
	        Result result=new Result();
	        try {
	            result=eduPlanListService.delete(eduPlanList.getId());
	        } catch (Exception e) {
	            e.printStackTrace();
	            result.setFalse(e.getMessage());
	        }
	        return result;
	    }
	  
	 
	  @RequestMapping(value = "/find", method = {RequestMethod.POST})
	  public Result find(@RequestBody EduPlanList eduPlanList) {
	        Result result = new Result();
	        try {
	            result = eduPlanListService.findDetail(eduPlanList);
	        } catch (Exception e) {
	            e.printStackTrace();
	            result.setFalse(e.getMessage());
	        }
	        return result;
	    }
	  
	  @RequestMapping(value = "/findSimple", method = {RequestMethod.POST})
	  public Result findSimple(@RequestBody EduPlanList eduPlanList) {
	        Result result = new Result();
	        try {
	            result = eduPlanListService.find(eduPlanList);
	        } catch (Exception e) {
	            e.printStackTrace();
	            result.setFalse(e.getMessage());
	        }
	        return result;
	    }
	  
	  @RequestMapping(value = "/findSubcompany", method = {RequestMethod.POST})
	  public Result findSubcompany(@RequestBody EduPlanList eduPlanList) {
	        Result result = new Result();
	        try {
	            result = eduPlanListService.findSubcompany(eduPlanList);
	        } catch (Exception e) {
	            e.printStackTrace();
	            result.setFalse(e.getMessage());
	        }
	        return result;
	    }
	  
	    @SysLog("导入教育培训计划")
	    @RequestMapping(value = "/addBatch",method = {RequestMethod.POST})
	    public Result addBatch(HttpServletRequest request, EduPlanList eduPlanList){
	        Result result = new Result();
	        try {
	            // 解析器解析request的上下文未登录或者过期
	            CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(request.getSession().getServletContext());
	            // 先判断request中是否包含mutipart类型的数据
	            if (multipartResolver.isMultipart(request)) {
	                // 再将request中得数据转化成multipart类型的数据f
	                MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request;
	                List<MultipartFile> multipartFiles = multiRequest.getFiles("file");
	                Iterator iterator = multiRequest.getFileNames();
	                while (iterator.hasNext()) {
	                    MultipartFile file = multiRequest.getFile((String) iterator.next());
	                    result = eduPlanListService.addBatch(file, eduPlanList);
	                    if (result.isSuccess()) {
	                    } else {
	                        result.setFalse(result.getMessage());
	                    }
	                }
	            }
	        } catch (Exception e) {
	            e.printStackTrace();
	            result.setFalse("导入失败" + e.getMessage());
	        }
	        return result;
	    }
	    
	    
//	    @SysLog("导入教育培训计划")
//	    @RequestMapping(value = "/addBatch",method = {RequestMethod.POST})
//	    public Result addBatch(HttpServletRequest request,@RequestBody EduPlanList eduPlanList){
//    	Result result=new Result();
//        try {
//	        result = eduPlanListService.addBatch(multipartFile,eduPlanList); 
//        } catch (Exception e) {
//            e.printStackTrace();
//            result.setFalse(e.getMessage());
//        }
//        return result;
//		}
	    
}
